import { createStore } from 'vuex'

export default createStore({
  state: {
    user: [],
    menu: []
  },
  getters: {
    getUser(state){
      console.log("getter方法被调用：getUser");
      return state.user;
    } ,
    getMenu(state){
      console.log("getter方法被调用：getMenu");
      return state.menu;
    } ,
    //isLogin: state => state.user&&state?.user?.length>0?true:false
    isLogin(state){
      //console.log("getter方法：",state.user);
      //console.log(state.user.length);
      return state.user?true:false;
    }
  },
  mutations: {
    setUser(state,user){
      //console.log("mutations方法中："+user.userId);
      state.user = user;
      //console.log("state.user:",state.user);
    },
    clearUser(state){
      state.user = null;

    },
    setMenu(state,menu){
      state.menu = menu;

    },
    clearMenu(state){
      state.menu = null;
    }
  },
  actions: {
    login({commit},user){
      //console.log("actions方法中："+user.userId);
      commit('setUser',user);
      sessionStorage.setItem("user",JSON.stringify(user));
    },
    logout(){
      this.commit('clearUser');
    },
  },
  modules: {
  },
  plugins:[
    // createPersistedState({
    //   path: ['user']
    // })
  ]
})
